package com.bjsj.pro.tools;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.mm.opensdk.utils.Log;

import java.io.File;
import java.util.ArrayList;

public class ShareUtils {

    /**
     * 微信7.0版本号，兼容处理微信7.0版本分享到朋友圈不支持多图片的问题
     */
    private static final int VERSION_CODE_FOR_WEI_XIN_VER7 = 1380;
    /**
     * 微信包名
     */
   public static final String PACKAGE_NAME_WEI_XIN = "com.tencent.mm";

    PopupWindow popupWindow;
    Context context;
    private String path;//单张图片路径
    private String content;
    private Button btn;
    private int typefx;
    private IWXAPI mIWXAPI;
    private int id;
    private String token;
    private Uri[] uris;//多张图片路径uri数组
    public ShareUtils(Context context,String content,int id,String token,int typefx){
        this.context=context;
        //  this.path=path;
          this.content=content;
          this.id=id;
          this.token=token;
          this.typefx=typefx;
        //  this.btn=btn;

        showChosePayTypeDialog();
    }

    public void setUri(Uri[] uris){
        this.uris = uris;
    }

    public void setPath(String path){
        this.path = path;
    }

    /**选择分享方式弹窗*/
    ChooseShareWxImgDialog chooseShareTypeDialog;
    public void showChosePayTypeDialog() {
        if (chooseShareTypeDialog == null) {
            chooseShareTypeDialog = new ChooseShareWxImgDialog(context);
        }

        chooseShareTypeDialog.show();

        chooseShareTypeDialog.setChoseTypeListener(new ChooseShareWxImgDialog.setChoseTypeListener() {
            @Override
            public void onChoseType(int type) {
                String appId = "wxe24cffd1a0fd11ab";
                mIWXAPI = WXAPIFactory.createWXAPI(context, appId, true);
                mIWXAPI.registerApp(appId);
              //  EventBus.getDefault().register(this);
                if (type == 1){//微信
                    if (!mIWXAPI.isWXAppInstalled()) {

                        Toast.makeText(context, "请先安装微信", Toast.LENGTH_SHORT).show();
                    } else {
                        Log.d("mmmmmmmmmmmmm",String.valueOf(uris.length));
                        shareWXSomeImg(context,uris);
                    }
                } else if (type == 2) {//朋友圈
                    if (!mIWXAPI.isWXAppInstalled()) {
                        Toast.makeText(context, "请先安装微信", Toast.LENGTH_SHORT).show();
                    } else {
                        Log.d("nnnnnnnnnnnnnnn",String.valueOf(uris.length));
                        if(typefx==1) {
                           // fx();
                        }else{
                          //  trainfx();
                        }
                        shareweipyqSomeImg(context,uris);//拉起微信朋友圈带多张图片
                    }
                }
            }
        });
    }




//    private void showpop() {
//        View view= LayoutInflater.from(context).inflate(
//                R.layout.share_view, null);
//        ImageView img_weixin= (ImageView) view.findViewById(R.id.share_weixin);
//        ImageView img_pyq= (ImageView) view.findViewById(R.id.share_pengyouquan);
//        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,
//                ViewGroup.LayoutParams.WRAP_CONTENT, true);
//        popupWindow.setBackgroundDrawable(new BitmapDrawable()); // 点击返回按钮popwindow消失
//
//        img_weixin.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (StringUtils.isWeixinAvilible(context)) {// 判断是否安装微信客户端
//                    // shareweixin(path);
//                    shareWXSomeImg(context,uris);
//                    // login(SHARE_MEDIA.WEIXIN);
//                } else {
//                    ActivityUtil.showToast(context, "请安装微信客户端");
//                }
//
//                popupWindow.dismiss();
//            }
//        });
//        img_pyq.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                if (StringUtils.isWeixinAvilible(context)) {// 判断是否安装微信客户端
//                    //   shareweipyq(path,content);//拉起微信朋友圈带一张图片
//                    shareweipyqSomeImg(context,uris);//拉起微信朋友圈带多张图片
//                    // login(SHARE_MEDIA.WEIXIN);
//                } else {
//                    ActivityUtil.showToast(context, "请安装微信客户端");
//                }
//                popupWindow.dismiss();
//            }
//        });
//
//        popupWindow.showAtLocation( LayoutInflater.from(context).inflate(
//                R.layout.activity_posterxq, null).findViewById(R.id.img_share), Gravity.BOTTOM, 0, 0);// 先设置popwindow的所有参数，最后再show
//
//    }

    /**
     * 拉起微信好友发送单张图片
     * */
    private void shareweixin(String path){
        Uri uriToImage = Uri.fromFile(new File(path));
        Intent shareIntent = new Intent();
        //发送图片到朋友圈
        //ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
        //发送图片给好友。
        ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
        shareIntent.setComponent(comp);
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
        shareIntent.setType("image/jpeg");
        context.startActivity(Intent.createChooser(shareIntent, "分享图片"));
    }
    /**
     * 拉起微信朋友圈发送单张图片
     * */
    private void shareweipyq(String path,String content){
        Uri uriToImage = Uri.fromFile(new File(path));
        Intent shareIntent = new Intent();
        //发送图片到朋友圈
        ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
        //发送图片给好友。
//        ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
        shareIntent.setComponent(comp);
        shareIntent.putExtra("Kdescription", content);
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
        shareIntent.setType("image/jpeg");
        context.startActivity(Intent.createChooser(shareIntent, "分享图片"));
    }

    /**
     * 拉起微信朋友圈发送多张图片
     * */
    private void shareweipyqSomeImg(Context context,Uri[] uri){
        Intent shareIntent = new Intent();
        //1调用系统分享
        // shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //2添加图片数组
        ArrayList<Uri> imageUris = new ArrayList<>();
        for (int i = 0; i < uri.length; i++) {
            imageUris.add(uri[i]);
            Log.e("shareIntent",uri[i]+"路径");
        }
        if (getVersionCode(context,PACKAGE_NAME_WEI_XIN) < VERSION_CODE_FOR_WEI_XIN_VER7) {
            // 微信7.0以下版本
            shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
            shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,imageUris);
        } else {
            // 微信7.0及以上版本,朋友圈只支持单张图片分享
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM,uri[0]);
        }
        shareIntent.setType("image/*");
        //3指定选择微信
        ComponentName componentName = new ComponentName("com.tencent.mm","com.tencent.mm.ui.tools.ShareToTimeLineUI");
        shareIntent.setComponent(componentName);
        //4开始分享
        context.startActivity(Intent.createChooser(shareIntent,"分享图片"));
        /*Intent intent = new Intent();
        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
        intent.putExtra("Kdescription", "wwwwwwwwwwwwwwwwwwww");
        ComponentName comp = new ComponentName("com.tencent.mm",
                "com.tencent.mm.ui.tools.ShareToTimeLineUI");
        intent.setComponent(comp);
        intent.setAction(Intent.ACTION_SEND_MULTIPLE);
        intent.setType("image/*");
        context.startActivity(intent);*/
    }
    /**
     * 拉起微信发送多张图片给好友
     * */
    private void shareWXSomeImg(Context context,Uri[] uri){
        Intent shareIntent = new Intent();
        //1调用系统分析
        shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //2添加图片数组
        ArrayList<Uri> imageUris = new ArrayList<>();
        for (int i = 0; i < uri.length; i++) {
            imageUris.add(uri[i]);
        }
        shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,imageUris);
        shareIntent.setType("image/*");
        //3指定选择微信
        ComponentName componentName = new ComponentName("com.tencent.mm","com.tencent.mm.ui.tools.ShareImgUI");
        shareIntent.setComponent(componentName);
        //4开始分享
        context.startActivity(Intent.createChooser(shareIntent,"分享图片"));
    }
    /**
     * 获取制定包名应用的版本的versionCode
     * @param context
     * @param
     * @return
     */
    public static int getVersionCode(Context context,String packageName) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(packageName, 0);
            int version = info.versionCode;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
